package com.gridmobi.xrtb;

import com.alibaba.fastjson.JSONObject;
import com.gridmobi.xrtb.domain.dsp.DspTrackLog;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 分解国家城市对照表 到单独文件 ，这样减少文件大小 支持前端下拉框选择
 * 国家城市对照 参照谷歌city列表
 */
public class FileUtil {


    public static void main(String[] args) throws Exception {
        Path fpath = Paths.get("/Users/huangyongchao/Downloads/55.log");
        Path wpath = Paths.get("/Users/huangyongchao/Downloads/55w.log");

        //创建BufferedReader
        try {
            BufferedReader bfr = Files.newBufferedReader(fpath);
            BufferedWriter bfw = Files.newBufferedWriter(wpath);

            AtomicInteger a = new AtomicInteger(0);
            bfr.lines().forEachOrdered(line -> {
                if(line.indexOf("{\"")>0){
                    int index = line.indexOf("{\"");
                    DspTrackLog trackArgs = JSONObject.parseObject(line.substring(index),DspTrackLog.class)        ;
                    a.incrementAndGet();
                    DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd");

/*
                    DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
*/
                    String date = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong(trackArgs.getStmp())),ZoneId.systemDefault()));

                    try {
                        bfw.write(date.concat(",").concat(trackArgs.getDevid()).concat(",").concat(trackArgs.getIp()).concat(",").concat(trackArgs.getAppbundle()));
                        bfw.newLine();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            });
            bfw.flush();
            System.out.println(a.get());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
